package com.itheima.reggie.controller;


import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    //接收短信
    @PostMapping("user/sendMsg")
    public ResultInfo sendSms(@RequestBody Map<String , String> map){
        //接收参数
        String phone = map.get("phone");
        //调用uservice的方法
        userService.sendSms(phone);

        return ResultInfo.success("短信发送成功");
    }

    //登录
    @PostMapping("user/login")
    public ResultInfo login(@RequestBody Map<String, String> map){
        //接受参数
        String phone = map.get("phone");
        String code= map.get("code");
        //调用service方法进行登录
        ResultInfo resultInfo = userService.login(phone,code);
        return resultInfo;

    }
    //退出
    @PostMapping("/user/logout")
    public ResultInfo logout(@RequestHeader("Authorization") String token){
        //1. 获取token
        token= token.replace("Bearer","").trim();
        //2. 调用service清理redis中当前token信息
        userService.remove(token);
        //3. 返回结果
        return ResultInfo.success(null);
    }

}
